import { createRouter, createWebHistory, RouteRecordRaw, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

interface RouteMeta {
  isShow: boolean,
  title: string,
  icon: string
}

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      // @ts-ignore
      title: '登录',
    },
    component: () => import(/* webpackChunkName: "login" */ '../views/loginView.vue')
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        meta: {
          isShow: true,
          // @ts-ignore
          title: '首页',
          icon: "Reading",
        },
        component: () => import(/* webpackChunkName: "goods" */ '../views/indexView.vue')
      },
      {
        path: '/goods',
        name: 'goods',
        meta: {
          isShow: true,
          // @ts-ignore
          title: '商品管理',
          icon: "SetUp",
        },
        component: () => import(/* webpackChunkName: "goods" */ '../views/GoodsView.vue')
      },
      {
        path: '/user',
        name: 'user',
        meta: {
          isShow: true,
          // @ts-ignore
          title: '用户管理',
          icon: "UserFilled",
        },
        redirect:'/users',
        children:[
          {
            path: '/users',
            name: 'users',
            meta: {
              isShow: false,
              // @ts-ignore
              title: '用户列表',
            },
            component: () => import(/* webpackChunkName: "user" */ '../views/UserView.vue')
          }
        ]
      },
      {
        path: '/role',
        name: 'role',
        meta: {
          isShow: true,
          // @ts-ignore
          title: '角色管理',
          icon: "Setting",
        },
        redirect: '/roles',
        children: [
          {
            path: '/roles',
            name: 'roles',
            meta: {
              // @ts-ignore
              title: '角色列表',
            },
            component: () => import(/* webpackChunkName: "role" */ '../views/RoleView.vue'),
          }
        ]
      },
      {
        path: '/authority',
        name: 'authority',
        meta: {
          isShow: false,
          // @ts-ignore
          title: '权限控制',
          icon: "Tickets",
        },
        component: () => import(/* webpackChunkName: "authority" */ '../views/AuthorityView.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)',
    redirect: '/error',
  },
  {
    path: '/error',
    name: 'error',
    meta: {
      // @ts-ignore
      title: 'error',
    },
    component: () => import(/* webpackChunkName: "error" */ '../views/ErrorView.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {

  if (to.meta.title) {
    // @ts-ignore：无法被执行的代码的错误：无法被执行的代码的错误
    document.title = to.meta.title
  }

  const token: string | null = localStorage.getItem("token")
  if (!token && to.path !== "/login") {
    next("/login")
  }
  else {
    next()
  }
})



export default router
